import {
  AccountParams,
  DeptListItem,
  MenuParams,
  RoleParams,
  RolePageParams,
  MenuListGetResultModel,
  DeptListGetResultModel,
  AccountListGetResultModel,
  RolePageListGetResultModel,
  RoleListGetResultModel,
} from './model/systemModel';
import { defHttp } from '@/utils/http/axios';

enum Api {
  AccountList = '/sys/user/page',
  IsAccountExist = '/system/accountExist',
  DeptList = '/system/getDeptList',
  setRoleStatus = '/system/setRoleStatus',
  MenuList = '/sys/permission/page',
  RolePageList = '/sys/role/page',
  GetAllRoleList = '/system/getAllRoleList',
  deleteMenuApi = '/sys/permission/delete',
  setPermissionsApi = '/sys/role/setPermissions',
  registerApi = '/sys/user/register',
  setRolesApi = '/sys/user/setRoles',
  permissionTreeApi = '/sys/permission/permissionTree',
  deleteRoleApi = '/sys/role/delete',
  addRoleApi = '/sys/role/add',
  unableAPI = '/sys/user/unable',
  enableAPI = '/sys/user/enable',
  getPermissionsByRoleIdAPI = '/sys/permission/getPermissionsByRoleId',
}

export const getAccountList = (params) => {
  params['current'] = params.page;
  params['size'] = params.pageSize;
  return defHttp
    .post(
      { url: Api.AccountList, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      const { records, total } = res.data;
      return {
        items: records, // 将数据列表赋值给 items
        total, // 直接使用 total
      };
    });
};

export const deleteMenu = (params: AccountParams) =>
  defHttp
    .get(
      { url: Api.deleteMenuApi + '?id=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });

//查询权限

export const getPermissionsByRoleIdAPI = (params) =>
  defHttp
    .get(
      { url: Api.getPermissionsByRoleIdAPI + '?roleId=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
//禁用账号
export const unableAPI = (params: AccountParams) =>
  defHttp
    .get(
      { url: Api.unableAPI + '?userId=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
// 启用账号

export const enableAPI = (params: AccountParams) =>
  defHttp
    .get(
      { url: Api.enableAPI + '?userId=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const queryUserGroupAllInfo = () =>
  defHttp
    .post(
      { url: '/sys/user/group/queryUserGroupAllInfo' },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return {
        items: res.data, // 将数据列表赋值给 items
      };
    });
export const deleteRole = (params: AccountParams) =>
  defHttp
    .post(
      { url: Api.deleteRoleApi + '?roleId=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const permissionTree = (params) =>
  defHttp
    .get(
      { url: Api.permissionTreeApi + '?type=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const setPermissions = (params) =>
  defHttp
    .post(
      { url: Api.setPermissionsApi, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const setaccountbind = (params) =>
  defHttp
    .post(
      { url: '/sys/task/sys/user/account/bind', params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const getfacebooklist = () =>
  defHttp
    .get(
      { url: '/sys/task/sys/user/account/facebook/no/bind/list' },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const getgetUserById = (params) =>
  defHttp
    .get(
      { url: '/sys/user/getUserById?userId=' + params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const addRole = (params) =>
  defHttp
    .post(
      { url: Api.addRoleApi, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });

export const setRoles = (params) =>
  defHttp
    .post(
      { url: Api.setRolesApi, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const register = (params) =>
  defHttp
    .post(
      { url: Api.registerApi, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });

export const userGroupregister = (params) =>
  defHttp
    .post(
      { url: '/sys/user/userGroup/register', params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      return res;
    });
export const getDeptList = (params?: DeptListItem) =>
  defHttp
    .get(
      { url: Api.DeptList, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      const { records, total } = res.data;
      return {
        items: records, // 将数据列表赋值给 items
        total, // 直接使用 total
      };
    });
export const getMenuList = (params) => {
  params['current'] = params.page;
  params['size'] = params.pageSize;
  return defHttp
    .post(
      { url: Api.MenuList, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      const { records, total } = res.data;
      // 将 records 中的每个 meta 字段转换为 JSON 对象
      const items = records.map((record) => {
        if (record.meta) {
          record.meta = JSON.parse(record.meta);
        }
        return record;
      });
      return {
        items, // 将转换后的数据列表赋值给 items
        total, // 直接使用 total
      };
    });
};

export const getRoleListByPage = (params?: RolePageParams) =>
  defHttp
    .post(
      { url: Api.RolePageList, params },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      const { records, total } = res.data;
      return {
        items: records, // 将数据列表赋值给 items
        total, // 直接使用 total
      };
    });

export const getAllRoleList = (params?: RoleParams) =>
  defHttp.get<RoleListGetResultModel>({ url: Api.GetAllRoleList, params });

export const setRoleStatus = (id: number, status: string) =>
  defHttp.post({ url: Api.setRoleStatus, params: { id, status } });

export const isAccountExist = (account: string) =>
  defHttp.post({ url: Api.IsAccountExist, params: { account } }, { errorMessageMode: 'none' });
